function [ POtimum, costs  ] = fullSearch(  X, P, W, Y,  MAXIT, tol, LB, WB )
%fullSearch is a function to perform full search through the parameters P in the bounds LB and WB
%  X is the raw data, Y is the target feature, MAXIT is the number of iterations,
%the vector P = {w1,l1;w2,l2;w3,l3;...;wn,ln}
%RETURN: Parameters P, Cost is the cost function for each iteration

%get p dimenssions
[Mp, Np] = size(P);

%number of features
N = size(X,2);

%store the P*
POtimum = P(:);

%used to search
Psrc = P(:);

%calculate the cost for the initialized P
[H, globalCost] = modelPrediction(Y ,X, P, W);

%create a vector to store the cost function in each iteration
costs = globalCost;

%count the number of cicles without change in the parameters
unchangeCount = 0;


%loop until pass MAXIT cicles without change
while(unchangeCount < MAXIT)
    
    %flag to mark if a changing happen during the cicle
    wasChanged = 0;
    
    %loop through the parameters
    for i = 1:(Mp*Np)
        
        %get limits of the parameters based on it is windows or historic
        %days
        if(i  <=  N)
            lb = WB(1);
            ub = WB(2);
            
        else
            lb = LB(1);
            ub = LB(2);
        end;
        
        %assign actual optimum
        Psrc = POtimum;
        
        costVar = inf;
        
        %loop throug possible values
        for v =lb:ub
            
            %try a valeu
            Psrc(i) = v;
            
            %calculate new features
            [FY, F] = featureProgram(Y, X, reshape(Psrc,N,2));
            
            %calculate the hypothesis. obs: intercept term
            M = size(F,1);
            H = [ones(M,1) F] * W;
            
            %calculate the cost
            cost = (1/(2*M)) .* ( sum((H - FY) .^ 2));
            
            %if the cost improve save the new parameter value and cost
            if(cost < costVar)
                %update optimums
                costVar = cost;
                POtimum(i) = Psrc(i);
                
            end;
            
        end;
        
        %mark as happened a change
        if(costVar < globalCost & globalCost - costVar > tol )
            globalCost = costVar;
            wasChanged = 1;
        end;
        %save cost
        costs = [costs; globalCost];
    end;
    
    %if no change occurs in this cicle
    if(~wasChanged)
        %increase number of cicles unchanged
        unchangeCount = unchangeCount + 1;
    else
        %otherwise: change happened set the count to zero
        unchangeCount = 0;
    end;
end;

%reshape the p parameters to Mx2 matrix
POtimum = reshape(POtimum,N,2);

end

